package org.ihelpuoo.orm;

import org.ihelpuoo.lang.StringUtils;
import org.ihelpuoo.util.Properties;

public class IIIConfig extends Properties
{

	private String		tablePrefix;
	private String[]	suffixes;
	private char		wordSeparator;

	public IIIConfig(Properties p)
	{
		super(p);
		tablePrefix = this.getStringTrimed("table-prefix");
		suffixes = StringUtils.splitIgnoreBlank(this.getStringTrimed("object-suffix"));
		String s = this.getStringTrimed("word-separator","");
		if (s.length() > 0)
			wordSeparator = s.charAt(0);
		else
			wordSeparator = (char)0;
	}

	public String evalObjectName(Class type)
	{
		return StringUtils.firstLowercase(peelOffSuffix(type.getSimpleName()));
	}

	String peelOffSuffix(String name)
	{
		String lower = name.toLowerCase();
		String re = null;
		if (null != suffixes && suffixes.length > 0)
		{
			for (int i = 0; i < suffixes.length; i++)
			{
				String s = suffixes[i];
				if (s.length() >= lower.length())
					continue;
				int p = 0;
				int p2 = lower.length() - s.length();
				boolean matched = true;
				for (; p < s.length(); p++)
					if (s.charAt(p) != lower.charAt(p2++))
					{
						matched = false;
						break;
					}
				if (matched)
				{
					re = name.substring(0,name.length() - s.length());
					break;
				}
			}
		}
		return null == re ? name : re;
	}

	public char getWordSeparator()
	{
		return wordSeparator;
	}

	public String getTablePrefix()
	{
		return tablePrefix;
	}

}
